<?php

namespace App\HttpController;

use App\Service\LoginService;
use EasySwoole\HttpAnnotation\AnnotationTag\Api;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiDescription;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiFail;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroup;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiSuccess;
use EasySwoole\HttpAnnotation\AnnotationTag\Method;
use EasySwoole\HttpAnnotation\AnnotationTag\Param;
use EasySwoole\Mysqli\Exception\Exception;
use Throwable;

/**
 * @ApiGroup(groupName="用户登录")
 */
class Login extends BaseController
{
    public function phoneLogin($phone, $password)
    {

    }

    /**
     * @ApiDescription(value="手机验证码登录")
     * @Api(name="手机验证码登录", path="/login/codeLogin", version="1.0.0")
     * @Method(allow={POST})
     * @Param(name="phone", description="手机号", required="手机号不能为空", notEmpty="手机号不能为空", from={JSON})
     * @Param(name="code", description="验证码", required="验证码不能为空", notEmpty="验证码不能为空", from={JSON})
     * @Param(name="requestId", description="验证码匹配任务ID", from={JSON})
     * @ApiSuccess({"code": "200", "result": {"id": "id", "userid": "用户id", "we_code": "微信唯一code", "phone": "手机号", "username": "昵称", "avatar": "头像", "house": "房屋", "type": "类型", "sex": "性别", "login_time": "上次登录时间", "login_ip": "上次登录ip", "token": "用户token",}, "msg": "登陆成功"})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function codeLogin($phone, $code, $requestId): bool
    {
        $ips = $this->request()->getHeaders()['x-real-ip'] ?? [];
        $this->ip = $ips[0] ?? '127.0.0.1';
        $res = LoginService::getInstance()->codeLogin($phone, $code, $requestId, $this->ip);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="微信一键登录")
     * @Api(name="微信一键登录", path="/login/weLogin", version="1.0.0")
     * @Method(allow={POST})
     * @Param(name="username", description="微信昵称", from={JSON})
     * @Param(name="avatar", description="微信头像", from={JSON})
     * @Param(name="sex", description="性别", from={JSON})
     * @Param(name="we_code", description="微信唯一code", from={JSON})
     * @ApiSuccess({"code": "200", "result": {"id": "id", "userid": "用户id", "we_code": "微信唯一code", "phone": "手机号", "username": "昵称", "avatar": "头像", "house": "房屋", "type": "类型", "sex": "性别", "login_time": "上次登录时间", "login_ip": "上次登录ip", "token": "用户token",}, "msg": "登陆成功"})
     * @return bool
     * @throws Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws Throwable
     */
    public function weLogin($username, $avatar, $sex, $we_code): bool
    {
        $ips = $this->request()->getHeaders()['x-real-ip'] ?? [];
        $this->ip = $ips[0] ?? '127.0.0.1';
        $res = LoginService::getInstance()->weLogin($username, $avatar, $sex, $we_code, $this->ip);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="账号密码登录")
     * @Api(name="账号密码登录", path="/login/passLogin", version="1.0.0")
     * @Method(allow={POST})
     * @Param(name="username", description="账号，或者手机号均可", from={JSON})
     * @Param(name="password", description="登录密码", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function passLogin($username, $password): bool
    {
        $ips = $this->request()->getHeaders()['x-real-ip'] ?? [];
        $this->ip = $ips[0] ?? '127.0.0.1';
        $res = LoginService::getInstance()->passLogin($username, $password, $this->ip);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }
}